package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 8033
 * http://www.mmmca.com/ 北美中文博客
 * @author KK
 *
 */
public class MmmcaBlogAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://www.mmmca.com/blog_[a-z0-9]*/p/[0-9]*.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		Integer deliverNum = null;
		String updateUrl = url;
		String author= null;
		Document doc = Jsoup.parse(htmltxt);

		author = doc.select("font#pageTitle").text();
		if(StringUtils.isBlank(author)){
			author = doc.select("h1.blogtitle").text();
		}
		title = doc.select("div#content div.entry div h2 a").text();
		if(StringUtils.isBlank(title))
		title = doc.select("h1").text();

		date = DateUtils.matchDate(doc.select("div.bPosts > h2, div.bPost > div.bSmallHead").text()) ;
		if (date==null) {
			date = DateUtils.matchDate(doc.select("div.infos").text()) ;
		}
		if (date==null) {
			date = DateUtils.matchDate(doc.select("div:containsOwn(分类:)").text()) ;
		}
		if(date == null){
			date = DateUtils.matchDate(doc.select("h2").text());
		}

		content = HtmlCleaner.getContentHtml(url,  doc.select("div.article"));
		if (StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url,  doc.select("div.bText"));
		}
		if(StringUtils.isBlank(content)){
			Elements select = doc.select("div.entry");
			select.select("p.postmetadata").remove();
			content = HtmlCleaner.getContentHtml(url, select);
		}

		clickNum =NumberUtils.matchNumber(doc.select("font:containsOwn(点击)").text());
		commnetNum =NumberUtils.matchNumber(doc.select("font:containsOwn(评分)").text());
		deliverNum = null;

			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(7);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setAuthor(author);
			news.setClickNum(clickNum);
			//news.setDeliverNum(deliverNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (meta!=null &&StringUtils.isNotBlank(meta.getUpdateUrl())){
			UrlMeta urlMeta = CrawlHTML.responseToURL(meta.getUpdateUrl());
			if (urlMeta!=null && urlMeta.getHtml()!=null){
				return parserHtml(urlMeta);
			}
		}
		return null;
	}

	

	
}
